package com.lpe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lpe.pojo.User;
import com.lpe.pojo.vo.ResultVo;
import com.lpe.service.UserInfoService;
import com.lpe.sys.SysCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * User控制层
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
//    @Autowired
//    private UserService userService;

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserInfoService UserInfoService;

    @RequestMapping(value = "/index")
    public String index() {
        return "user/index";
    }

    @RequestMapping(value = "/findUserById")
    @ResponseBody
    public ResultVo findUserById(@RequestParam(value = "id") Integer id) {
        logger.info("开始执行查询id为【{}】用户操作", id);
        User user = UserInfoService.findUserById(id);
        ResultVo resultVo = new ResultVo();
        if (null == user) {
            resultVo.setMsg("id:【" + id + "】对应的数据未找到");
            resultVo.setCode(SysCode.HTTP_CODE_ERROR);
        } else {
            resultVo.setMsg("success");
            resultVo.setCode(SysCode.HTTP_CODE_SUCCESS);
            resultVo.setData(user);
            logger.info(JSON.toJSONString(user));
        }
        return resultVo;
    }

    @RequestMapping(value = "/findUserById2")
    @ResponseBody
    public ResultVo findUserById2(@RequestBody String query,@RequestParam(value = "id") String id) {
        logger.info("开始执行查询用户操作参数为【{}】id：{}", query,id);
        JSONObject queryJson = JSON.parseObject(query);
        ResultVo resultVo = new ResultVo();
        User user = new User();
        if (null != queryJson.getInteger("id")) {
            user = UserInfoService.findUserById(queryJson.getInteger("id"));
            if (null == user) {
                resultVo.setMsg("参数:【" + query + "】对应的数据未找到");
                resultVo.setCode(SysCode.HTTP_CODE_ERROR);
            } else {
                resultVo.setMsg("success");
                resultVo.setCode(SysCode.HTTP_CODE_SUCCESS);
                resultVo.setData(user);
                logger.info(JSON.toJSONString(user));
            }
        }
        return resultVo;
    }

    @RequestMapping(value = "/findUserById3/{id}")
    @ResponseBody
    public ResultVo findUserById3(@PathVariable("id") Integer id) {
        logger.info("开始执行查询id为【{}】用户操作", id);
        ResultVo resultVo = new ResultVo();
        User user = UserInfoService.findUserById(id);
        if (null == user) {
            resultVo.setMsg("参数:【" + id + "】对应的数据未找到");
            resultVo.setCode(SysCode.HTTP_CODE_ERROR);
        } else {
            resultVo.setMsg("success");
            resultVo.setCode(SysCode.HTTP_CODE_SUCCESS);
            resultVo.setData(user);
            logger.info(JSON.toJSONString(user));
        }
        return resultVo;
    }

    @RequestMapping(value = "/insertDemo")
    @ResponseBody
    public User insertDemo() {
        return UserInfoService.insertUser(null);
    }
}
